package com.ts.proj.json;

import com.ts.base.controller.BaseController;
import com.ts.common.constant.InsUpdDelFlagConstant;
import com.ts.common.constant.SystemButtonTableColumnConstant;
import com.ts.common.constant.SystemUserTableColumnConstant;
import com.ts.proj.entity.SystemButtonEntity;
import com.ts.proj.entity.SystemMenuEntity;
import com.ts.proj.service.SystemButtonService;
import com.ts.proj.service.SystemMenuService;
import com.ts.util.DateUtil;
import com.ts.util.PageData;
import com.ts.util.RightsHelper;
import com.ts.util.TreeMenuUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@Slf4j
public class SystemButtonJson extends BaseController {


    @Autowired
    private SystemButtonService systemButtonService;

    @Autowired
    private SystemMenuService systemMenuService;

    @RequestMapping("/btn-save/{id}/{menuId}")
    public Map<String,Object> menuSave(@PathVariable("id") String id,@PathVariable("menuId") String menuId){
        PageData pageData = this.getPageData();
        SystemButtonEntity systemButtonEntity = systemButtonService.findById(Integer.valueOf(id));
        if(null!=pageData.get("btn_name")&&!"".equals(pageData.get("btn_name").toString())){
            systemButtonEntity.setButtonName(pageData.getString("btn_name"));
        }
        if(null!=pageData.get("btn_remark")&&!"".equals(pageData.get("btn_remark").toString())){
            systemButtonEntity.setButtonRemark(pageData.getString("btn_remark"));
        }
        if(null!=pageData.get("btn_icon")&&!"".equals(pageData.get("btn_icon").toString())){
            systemButtonEntity.setIconCls(pageData.getString("btn_icon"));
        }
        if(null!=pageData.get("btn_id")&&!"".equals(pageData.get("btn_id").toString())){
            systemButtonEntity.setButtonId(pageData.getString("btn_id"));
        }
        if(null!=menuId&&!"".equals(menuId)&&!"-1".equals(menuId)){
            systemButtonEntity.setMenuID(Integer.valueOf(menuId));
        }
        SystemButtonEntity save = systemButtonService.save(systemButtonEntity);
        Map<String, Object> map = new HashMap<>();
        map.put("result", "success");
        map.put("parentId", save.getMenuID() + 1000);
        return map;
    }

    @RequestMapping("/button-ins-upd-del/{flag}")
    public String buttonInsUpdDel(@PathVariable("flag") String flag){
        PageData pageData = this.getPageData();
        //删除
        if(InsUpdDelFlagConstant.DELETE.equals(flag)){
            SystemButtonEntity byId = systemButtonService.findById(Integer.valueOf(pageData.get(SystemButtonTableColumnConstant.ID).toString()));
            byId.setDeleteFlag(InsUpdDelFlagConstant.DELETE_FLAG_Y);
            SystemButtonEntity save = systemButtonService.save(byId);
            return String.valueOf(save.getMenuID()+1000);
        }
        //新增
        if(InsUpdDelFlagConstant.INSERT.equals(flag)){
            SystemButtonEntity systemButtonEntity = new SystemButtonEntity();
            systemButtonEntity.setButtonName(pageData.getString(SystemButtonTableColumnConstant.BUTTONNAME));
            systemButtonEntity.setButtonRemark(pageData.getString(SystemButtonTableColumnConstant.BUTTONREMARK));
            systemButtonEntity.setButtonId(pageData.getString(SystemButtonTableColumnConstant.BUTTONID));
            systemButtonEntity.setIconCls(pageData.getString(SystemButtonTableColumnConstant.ICONCLS));
            systemButtonEntity.setCreateTime(DateUtil.getTime());
            systemButtonEntity.setMenuID(Integer.valueOf(pageData.get(SystemButtonTableColumnConstant.MENUNAME).toString()));
            systemButtonService.save(systemButtonEntity);
        }
        //修改
        if(InsUpdDelFlagConstant.UPDATE.equals(flag)){
            SystemButtonEntity systemButtonEntityById = systemButtonService.findById(Integer.valueOf(pageData.get(SystemUserTableColumnConstant.ID).toString()));
            systemButtonEntityById.setButtonName(pageData.getString(SystemButtonTableColumnConstant.BUTTONNAME));
            systemButtonEntityById.setButtonRemark(pageData.getString(SystemButtonTableColumnConstant.BUTTONREMARK));
            systemButtonEntityById.setButtonId(pageData.getString(SystemButtonTableColumnConstant.BUTTONID));
            systemButtonEntityById.setIconCls(pageData.getString(SystemButtonTableColumnConstant.ICONCLS));
            systemButtonEntityById.setUpdateTime(DateUtil.getTime());
            systemButtonService.save(systemButtonEntityById);
        }
        return "";

    }

    /**
     * 初始化按钮
     * @param rights
     * @return
     */
    @RequestMapping(value = "/initMenuButton/{rights}")
    public Map<String,List<PageData>> initMenuButton(@PathVariable("rights") String rights){
        Map<String,List<PageData>> map = new HashMap<String, List<PageData>>();
        PageData pd = this.getPageData();
        //按钮权限
        String btnRights = pd.getString("btnRights").substring(1);
        SystemMenuEntity systemMenuEntity = new SystemMenuEntity();
        systemMenuEntity.setId(0);
        systemMenuEntity.setParentId("0");
        List<SystemMenuEntity> allMenu = systemMenuService.findAll();
        List<PageData> result = new ArrayList<PageData>();
        result = new TreeMenuUtil().getTreeMenu(allMenu,systemMenuService);
        List<PageData> nodes = new ArrayList<PageData>();
        for(PageData tPageData : result ){
            if(tPageData.getString("PARENT_ID").equals("0")){
                int mu = Integer.valueOf(tPageData.get("MENU_ID").toString());
                if(!RightsHelper.testRights(rights.substring(1),mu)){
                    continue;
                }
                PageData parentPD = new PageData();
                parentPD.put("id",mu);
                parentPD.put("open", true);
                parentPD.put("pId",tPageData.getString("PARENT_ID"));
                parentPD.put("name",tPageData.getString("MENU_NAME"));
                parentPD.put("checked", RightsHelper.testRights(rights.substring(1),mu));
                nodes.add(parentPD);
                List<SystemMenuEntity> firstSubMenu = (List<SystemMenuEntity>) tPageData.get("firstSubMenu");
                for(SystemMenuEntity subPageData : firstSubMenu){
                    int smu = subPageData.getId();
                    if(!RightsHelper.testRights(rights.substring(1),smu)){
                        continue;
                    }
                    PageData firstPD = new PageData();
                    firstPD.put("id",smu);
                    firstPD.put("pId",subPageData.getParentId());
                    firstPD.put("name",subPageData.getMenuName());
                    firstPD.put("open", true);
                    firstPD.put("icon","/lib/zTree/v3/css/zTreeStyle/img/diy/7.png");
                    firstPD.put("checked", RightsHelper.testRights(rights.substring(1),smu));
                    nodes.add(firstPD);

                    //通过按钮
                    PageData pageData = new PageData();
                    pageData.put("menuID", smu);
                    List<SystemButtonEntity> allOrOne = systemButtonService.findAll(pageData,0,500);
                    for(SystemButtonEntity sBtn : allOrOne){
                        PageData buttonPd = new PageData();
                        buttonPd.put("id", 200+sBtn.getId());
                        buttonPd.put("pId", smu);
                        buttonPd.put("name", sBtn.getButtonName());
                        buttonPd.put("checked", RightsHelper.testRights(btnRights,(200+sBtn.getId())));
                        nodes.add(buttonPd);
                    }

                }
            }
        }
        map.put("nodes",nodes);
        return map;
    }


}
